package dbmanager;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import framework.*;

public class DatabaseManager {
	final String databaseLocation = "jdbc:mysql://mysql.wpi.edu/nomdb";
	final String username = "thomas";
	final String password = "nyfRo9";
	Connection conn;
	
	// TESTING PURPOSES ONLY
	public static void main(String[] args) throws SQLException{
		DatabaseManager dbm = new DatabaseManager();
		dbm.connect();
	}
	
	/**
	 * Connect to the database
	 * @throws SQLException
	 */
	public void connect() throws SQLException {
		conn = DriverManager.getConnection(databaseLocation, username, password);		
	}
	
	/**
	 * Disconnects from the database
	 * @throws SQLException
	 */
	public void disconnect() throws SQLException {
		conn.close();
	}

	/**
	 * Send a query that you don't expect a response from
	 * @param query
	 * @throws SQLException
	 */
	public void sendInsertQuery(String query) throws SQLException {
		Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		stmt.execute(query);
	}
	
	/**
	 * Send a query and get back a result set
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public ResultSet sendQuery(String query) throws SQLException {
		Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		return stmt.executeQuery(query);
	}
	
	public ArrayList<String[]> getUsers() throws SQLException {
		ResultSet srs = sendQuery("select * from nom_user;");
		ArrayList<String[]> userList = new ArrayList<String[]>();
		while (srs.next()) {
	        String[] userInfo = new String[4];
			userInfo[0] = srs.getString("id");
			userInfo[1] = srs.getString("name");
			userInfo[2] = srs.getString("email");
			userInfo[3] = srs.getString("username");
			userList.add(userInfo);
		}
		return userList;
	}
	
	// Not done yet.
	public String insertRecipe(Recipe r) {
		return "insert into recipe values (" + r.getId() + ",";
	}
}
